import { ref } from 'vue'

export const useGetData = (api, params = {}, options = {}) => {
  const { defaultValue = [] } = options
  const data = ref([])
  api(params)
    .then(result => {
      data.value = result || defaultValue
    }).catch(error => {
      console.log(error)
      data.value = defaultValue
    })
  return {
    data
  }
}

export const useRequest = (api, params = {}, options = {}) => {
  const { defaultValue = [] } = options
  const data = defaultValue
  return new Promise(async (resolve, reject) => {
    try {
      const result = await api(params) || defaultValue
      resolve(result)
    } catch (error) {
      console.log(error)
      resolve(data)
    }
  })
}
